home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / CBASE.ZIP / CBKEYSRC.C < prev    next >
Text File  |  1990-06-21  |  4KB  |  168 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbkeysrc.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <errno.h>
  8. /*#include <stddef.h>*/
  9. /*#include <stdlib.h>*/
  10. /*#include <string.h>*/
  11.  
  12. /* non-ansi headers */
  13. #include <bool.h>
  14.  
  15. /* library headers */
  16. #include <blkio.h>
  17. #include <btree.h>
  18. #include <lseq.h>
  19.  
  20. /* local headers */
  21. #include "cbase_.h"
  22.  
  23. /*man---------------------------------------------------------------------------
  24. NAME
  25.      cbkeysrch - search cbase key
  26.  
  27. SYNOPSIS
  28.      #include <cbase.h>
  29.  
  30.      int cbkeysrch(cbp, field, buf)
  31.      cbase_t *cbp;
  32.      int field;
  33.      const void *buf;
  34.  
  35. DESCRIPTION
  36.      The cbkeysrch function searches a key in cbase cbp.  field is the
  37.      field to be searched.  buf points to the key for which to be
  38.      searched.  If found, the cursor for that key is positioned to the
  39.      found key.  If it is not found, the cursor for that key is
  40.      positioned to the next higher key, which may be null.  The record
  41.      cursor is set to the record associated with the key marked by the
  42.      key cursor.  Other key cursors are not affected.
  43.  
  44.      cbkeysrch will fail if one or more of the following is true:
  45.  
  46.      [EINVAL]       cbp is not a valid cbase pointer.
  47.      [EINVAL]       field is not a valid field number for
  48.                     cbase cbp.
  49.      [EINVAL]       buf is the NULL pointer.
  50.      [CBELOCK]      cbp is not read locked.
  51.      [CBENKEY]      field is not a key.
  52.      [CBENOPEN]     cbp is not open.
  53.  
  54. SEE ALSO
  55.      cbkcursor, cbkeyfirst, cbkeylast, cbkeynext, cbkeyprev.
  56.  
  57. DIAGNOSTICS
  58.      Upon successful completion, a value of 1 is returned if the key
  59.      was found or a value of 0 if it was not.  Otherwise, a value of
  60.      -1 is returned, and errno set to indicate the error.
  61.  
  62. ------------------------------------------------------------------------------*/
  63. int cbkeysrch(cbp, field, buf)
  64. cbase_t *cbp;
  65. int field;
  66. const void *buf;
  67. {
  68.     void *buf2 = NULL;
  69.     cbrpos_t cbrpos = NIL;
  70.     lspos_t lspos = NIL;
  71.     bool found = FALSE;
  72.  
  73.     /* validate arguments */
  74.     if (!cb_valid(cbp) || buf == NULL) {
  75.         errno = EINVAL;
  76.         return -1;
  77.     }
  78.  
  79.     /* check if not open */
  80.     if (!(cbp->flags & CBOPEN)) {
  81.         errno = CBENOPEN;
  82.         return -1;
  83.     }
  84.  
  85.     /* validate arguments */
  86.     if (field < 0 || field >= cbp->fldc) {
  87.         errno = EINVAL;
  88.         return -1;
  89.     }
  90.  
  91.     /* check if field is a key */
  92.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  93.         errno = CBENKEY;
  94.         return -1;
  95.     }
  96.  
  97.     /* check if not read locked */
  98.     if (!(cbp->flags & CBRDLCK)) {
  99.         errno = CBELOCK;
  100.         return -1;
  101.     }
  102.  
  103.     /* construct (key, record position) pair */
  104.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  105.         CBEPRINT;
  106.         errno = CBEPANIC;
  107.         return -1;
  108.     }
  109.     buf2 = calloc((size_t)1, cbp->fldv[field].len + sizeof(cbrpos_t));
  110.     if (buf2 == NULL) {
  111.         CBEPRINT;
  112.         errno = ENOMEM;
  113.         return -1;
  114.     }
  115.     cbrpos = NIL;
  116.     memcpy(buf2, buf, cbp->fldv[field].len);
  117.     memcpy(((char *)buf2 + cbp->fldv[field].len), &cbrpos, sizeof(cbrpos_t));
  118.  
  119.     /* search for key */
  120.     if (btsearch(cbp->btpv[field], buf2) == -1) {
  121.         CBEPRINT;
  122.         free(buf2);
  123.         return -1;
  124.     }
  125.  
  126.     /* check if cursor is null */
  127.     if (btcursor(cbp->btpv[field]) == NULL) {
  128.         free(buf2);
  129.         if (lssetcur(cbp->lsp, NULL) == -1) {
  130.             CBEPRINT;
  131.             return -1;
  132.         }
  133.         errno = 0;
  134.         return 0;
  135.     }
  136.  
  137.     /* get record position */
  138.     if (btgetk(cbp->btpv[field], buf2) == -1) {
  139.         CBEPRINT;
  140.         free(buf2);
  141.         return -1;
  142.     }
  143.     memcpy(&cbrpos, ((char *)buf2 + cbp->fldv[field].len), sizeof(cbrpos));
  144.  
  145.     /* check if key found or not */
  146.     if ((*cbcmpv[cbp->fldv[field].type])(buf, buf2, cbp->fldv[field].len) == 0) {
  147.         found = TRUE;
  148.     } else {
  149.         found = FALSE;
  150.     }
  151.     free(buf2);
  152.     buf2 = NULL;
  153.  
  154.     /* set record cursor */
  155.     lspos = cbrpos;
  156.     if (lspos == NIL) {
  157.         CBEPRINT;
  158.         errno = CBEPANIC;
  159.     }
  160.     if (lssetcur(cbp->lsp, &lspos) == -1) {
  161.         CBEPRINT;
  162.         return -1;
  163.     }
  164.  
  165.     errno = 0;
  166.     return (found ? 1 : 0);
  167. }
  168.